package org.NooLab.glue.net.sockets.tcp;

import java.util.Observable;
import java.util.Observer;

import org.NooLab.glue.components.DataReceiverIntf;
import org.NooLab.glue.components.DataSenderIntf;
import org.NooLab.glue.msgframe.message.ClientMsgHandlerIntf;
import org.NooLab.glue.msgframe.message.ServerMsgHandlerIntf;
import org.NooLab.glue.net.TransportBoxIntf;
import org.NooLab.glue.net.TransportsInformerIntf;
 	



public class TcpBox extends Observable implements TransportBoxIntf, GlueTcpletHandlerIntf{

	DataSenderIntf tcpSender;
	DataReceiverIntf tcpReceiver;
	
	TcpMessageHandlerFactory msgFactory ; 
	
	ServerMsgHandlerIntf serverMsg ;
	ClientMsgHandlerIntf clientMsg ;
	
	TransportsInformerIntf transportInformer;
	
	public TcpBox(){
		msgFactory = new TcpMessageHandlerFactory();
		
		serverMsg = msgFactory.getServerMsgHandler();
		clientMsg = msgFactory.getClientMsgHandler();
	}
	
	public DataReceiverIntf createReceiver( int listenport, int ctrlport, Observer observer, String description) {
		TcpServer tcpServer;
		int r=0;
		 
		
		tcpServer = new TcpServer( this, listenport, ctrlport, observer, serverMsg, description);
		r = tcpServer.activate() ;
		
		if (tcpServer.socketState==0){
			tcpReceiver = (DataReceiverIntf)tcpServer ;
		}else{
			tcpReceiver = null;
		}
		
		return tcpReceiver;
	} 
	
	public DataSenderIntf createSender(  int clientport, int ctrlport, String description ) {
		 
		tcpSender = new TcpClient( clientport, ctrlport, clientMsg, description );
		
		return tcpSender;
	}
	
	
	public void setTransportsInformer( TransportsInformerIntf informer ){
		transportInformer = informer ;
	}
	
	
	
}
